﻿using Library.Core.Domain;
using Library.Core.Domain.Specification;
using Synthesize.BaseSet.Domain.Aggregation.Entity.Authority;
using Synthesize.BaseSet.Domain.Repository.Authority;
using System.Threading.Tasks;

namespace Synthesize.BaseSet.Domain.Specifications.Authority.Menu
{
    public class DeleteMenuSpecification : IOperateSpecification<MenuInfoEntity>
    {
        private readonly IMenuRepository _menuRepository;

        public DeleteMenuSpecification(IMenuRepository menuRepository)
        {
            _menuRepository = menuRepository;
        }
        public async Task<bool> SatisfiedBy(MenuInfoEntity entity)
        {
            if (await _menuRepository.AnyAsync(x => x.ParentId.Equals(entity.Id)))
            {
                throw new DomainException("该菜单存在下级,不能删除");
            }
            return true;
        }
    }
}
